package com.wanho.qz103.hrsaas.sys.contoller;

import com.wanho.qz103.hrsaas.commons.annontations.*;
import com.wanho.qz103.hrsaas.commons.exception.ControllerException;
import com.wanho.qz103.hrsaas.commons.exception.ServiceException;
import com.wanho.qz103.hrsaas.commons.query.Page;
import com.wanho.qz103.hrsaas.commons.vo.R;
import com.wanho.qz103.hrsaas.commons.vo.TableVO;
import com.wanho.qz103.hrsaas.sys.pojos.Permission;
import com.wanho.qz103.hrsaas.sys.pojos.Role;
import com.wanho.qz103.hrsaas.sys.service.RoleService;
import com.wanho.qz103.hrsaas.sys.vo.RoleVO;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import sun.security.acl.PermissionImpl;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 角色模块：api接口调用 处理类
 */
@RestController
public class RoleController {

    @Autowired
    private RoleService roleService ;

    /**
     * 分配权限
     * @param request
     * @return
     * @throws ControllerException
     */
    @PutMapping("/sys/role/assignPrem")
    public R assignPrem(HttpServletRequest request) throws ControllerException{
        try {
            //角色id
            String id = request.getParameter("id");
            // 只读
            Map<String, String[]> parameterMap = request.getParameterMap();
            // 套娃 使用自己的map 可以删除元素
            Map<String, String[]> permsIdMap = new HashMap<>(parameterMap) ;
            // 删除 id
            permsIdMap.remove("id") ;
            // [{"x"},{"y"}] =>["x","y"] =>[new Permission("x"),new Permission("y")]
            List<Permission> permissionList = permsIdMap.values().stream().map(paramValue -> paramValue[0]).map(Permission::new).collect(Collectors.toList());
            permsIdMap.forEach((k,v)->{
                System.out.println("请求参数名:" + k + ",请求参数值:" + ArrayUtils.toString(v));
            });
            // 封装 Role
            Role role = new Role();
            role.setId(id);
            role.setPermissionList(permissionList);
            // 调用业务层
            boolean assignPrem = roleService.assignPrem(role);
            if (assignPrem) {
                return R.success("分配成功！！！") ;
            }
            return R.fail("分配权限失败！");
        } catch (ServiceException e) {
            e.printStackTrace();
            throw new ControllerException(e.getMessage()) ;
        }
    }

    /**
     * 角色列表 查询
     * @param request
     * @return
     * @throws ControllerException
     */
    @GetMapping("/sys/role")
    public R list(HttpServletRequest request) throws ControllerException {
        try {
            // page=1&pagesize=3
            String page = request.getParameter("page");
            String pagesize = request.getParameter("pagesize");
            Page page1 = new Page();
            if (StringUtils.isNotEmpty(page)) {
                page1.setPage(Long.parseLong(page));
            }
            if (StringUtils.isNotEmpty(pagesize)) {
                page1.setSize(Integer.parseInt(pagesize));
            }
            TableVO tableVO = roleService.findPage(page1);
            return R.success("查询角色列表成功！",tableVO) ;
        } catch (ServiceException e) {
            e.printStackTrace();
            throw new ControllerException("查询角色列表控制层异常！！！") ;
        }
    }

    /**
     * 新增角色
     * @param request
     * @return
     * @throws ControllerException
     */
    @PostMapping("/sys/role")
    public R save(HttpServletRequest request) throws ControllerException {
        return null ;
    }


    /**
     * 删除角色
     * @param request
     * @return
     * @throws ControllerException
     */
    @DeleteMapping("/sys/role/{id}")
    public R delete(HttpServletRequest request) throws ControllerException {
        //获得用户请求id
        return null ;
    }

    /**
     * 更新角色
     * @param request
     * @return
     * @throws ControllerException
     */
    @PutMapping("/sys/role/{id}")
    public R update(HttpServletRequest request) throws ControllerException {
        //获得用户请求id
        return null ;
    }

    /**
     * 查看角色
     * @param request
     * @return
     * @throws ControllerException
     */
    @GetMapping("/sys/role/{id}")
    public R view(HttpServletRequest request) throws ControllerException {
        try{
            //获得用户请求id
            String requestURI = request.getRequestURI();
            String[] split = requestURI.split("/");
            String id = split[split.length-1] ;
            RoleVO roleVO = roleService.findById(id);
            return R.success("查看角色详情成功！",roleVO) ;
        } catch (ServiceException e) {
            e.printStackTrace();
            throw new ControllerException("查询角色列表控制层异常！！！") ;
        }
    }

}
